/*
 * AbstractResourceWatcher.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.core.hotdeploy;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

public abstract class AbstractResourceWatcher extends IntervalThread implements
		IResourceWatcher {

	private Collection<IResourceListener> listeners = new LinkedList<IResourceListener>();

	public AbstractResourceWatcher(int intervalSeconds, String name) {

		super(intervalSeconds, name);
	}

	public void removeAllListeners() {

		listeners.clear();
	}


	public void addListener(IResourceListener listener) {

		listeners.add(listener);
	}


	public void removeListener(IResourceListener listener) {

		listeners.remove(listener);
	}


	protected void resourceAdded(Object newResource) {

		Iterator listIt = listeners.iterator();

		while (listIt.hasNext()) {
			((IResourceListener) listIt.next()).onAdd(newResource);
		}
	}


	protected void resourceChanged(Object changedResource) {

		Iterator listIt = listeners.iterator();

		while (listIt.hasNext()) {
			((IResourceListener) listIt.next()).onChange(changedResource);
		}
	}

	protected void resourceDeleted(Object deletedResource) {

		Iterator listIt = listeners.iterator();

		while (listIt.hasNext()) {
			((IResourceListener) listIt.next()).onDelete(deletedResource);
		}
	}


	protected void monitoringStarted(Object monitoredResource) {

		Iterator listIt = listeners.iterator();

		while (listIt.hasNext()) {
			((IResourceListener) listIt.next()).onStart(monitoredResource);
		}
	}


	protected void monitoringStopped(Object notMonitoredResource) {

		Iterator listIt = listeners.iterator();

		while (listIt.hasNext()) {
			((IResourceListener) listIt.next()).onStop(notMonitoredResource);
		}
	}


	protected abstract void doInterval();
}
